package m2;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
//        新建消费者实例
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("consumer2");
//        设置name server
        c.setNamesrvAddr("192.168.64.141:9876");
//        订阅消息
        /** 标签
         *  * ---所有标签
         *  tag1  --- 只接收tag1标签
         *  Tag1 || Tag2 || Tag3 ---接收Tag1,Tag2,Tag3的消息
         */
        c.subscribe("Topic2", "*");
//        设置消息监听器
        /**
         * Orderely ---按顺序消费
         */
        c.setMessageListener(new MessageListenerOrderly() {
           @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
               for (MessageExt msg : list) {
                   String s = new String (msg.getBody());
                   System.out.println("收到："+s);
               }
                           return ConsumeOrderlyStatus.SUCCESS;
                     }
                    });
        c.start();
//               返回消息处理状态(回执)
            }

}
